<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\resources\model;

/**
 * Description of VideoComment
 * 视频评论表
 * @author shilinqing
 */
class VideoComment extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'video_comment';
    }
    
    public function getMyCommentList($user_id, $pageIndex = -1, $pageSize = -1){
        $u = new \addons\member\model\MemberAccountModel();
        $r = new \addons\resources\model\Resources();
        $where['a.user_id'] = $user_id;
        if ($pageIndex > 0 && $pageSize > 0) {
            $offset = 0;
            if ($pageIndex > 0)
                $offset = (intval($pageIndex) - 1) * intval($pageSize);
            $this->limit($offset, $pageSize);
        }
        $this->alias('a')
            ->where($where)
            ->join($this->getTableName().' b' ,'a.pid=b.id','left')
            ->join($u->getTableName().' u', 'b.user_id=.u.id','left')
            ->join($r->getTableName().' r', 'a.video_id=r.id','left')
            ->field('a.*, b.comment as parent_comment,u.username as parent_username,r.alias_name,r.file_path');
        if ($pageIndex > 0 && $pageSize > 0) {
            $offset = 0;
            if ($pageIndex > 0)
                $offset = (intval($pageIndex) - 1) * intval($pageSize);
            $this->limit($offset, $pageSize);
        }
        $data = $this->select();
        return $data;
    }

    /**
     * 递归查询comment
     * @param type $video_id
     * @param type $depth -1 = 全部
     */
    public function recursiveComment($video_id, $user_id=0, $pid = 0, $pageIndex = -1, $pageSize = -1, &$result = array()) {
        $u = new \addons\member\model\MemberAccountModel();
        $where['video_id'] = $video_id;
        $where['pid'] = $pid;
        $this->where($where);
        if ($pageIndex > 0 && $pageSize > 0) {
            $offset = 0;
            if ($pageIndex > 0)
                $offset = (intval($pageIndex) - 1) * intval($pageSize);
            $this->limit($offset, $pageSize);
        }
        $is_like_fileds = '';
        if($user_id > 0){
            $is_like_fileds = ",if(c.id,'1',0) as is_like";
        }
        $sql = $this->fetchSql(1)->select();
        $sql = 'select a.*, b.username,b.head_img '.$is_like_fileds.' from (' . $sql . ') a left join ' . $u->getTableName() . ' b on a.user_id=b.id';
        if($user_id > 0){
            $likeM = new \addons\resources\model\VideoCommLikeRecord();
            $sql .= ' left join '.$likeM->getTableName().' c on c.user_id='.$user_id.' and FIND_IN_SET(a.id,comment_ids)';
        }
        $list = $this->query($sql);
        foreach ($list as $comm) {
            $thisArr = &$result[];
            $comm["children"] = $this->recursiveComment($video_id, $user_id, $comm['id'], $pageIndex, $pageSize, $thisArr);
            $thisArr = $comm;
        }
        return $result;
    }
    
    public function getTwoDepthComments($video_id, $pageIndex = -1, $pageSize = -1, $user_id = 0){
        $u = new \addons\member\model\MemberAccountModel();
        $where['video_id'] = $video_id;
        $where['pid'] = 0;
        $this->where($where);
        if ($pageIndex > 0 && $pageSize > 0) {
            $offset = 0;
            if ($pageIndex > 0)
                $offset = (intval($pageIndex) - 1) * intval($pageSize);
            $this->limit($offset, $pageSize);
        }
        $sql = $this->fetchSql(1)->select();
        $is_like_fileds = '';
        if($user_id > 0){
            $is_like_fileds = ",if(c.id,'1',0) as is_like";
        }
        $sql = 'select a.*, b.username,b.head_img '.$is_like_fileds.' from (' . $sql . ') a left join ' . $u->getTableName() . ' b on a.user_id=b.id';
        if($user_id > 0){
            $likeM = new \addons\resources\model\VideoCommLikeRecord();
            $sql .= ' left join '.$likeM->getTableName().' c on c.user_id='.$user_id.' and FIND_IN_SET(a.id,comment_ids)';
        }
        $list = $this->query($sql);
        foreach($list as $k => $v){
            $fields = 'a.*,b.username,b.head_img';
            $where1['video_id'] = $video_id;
            $where1['a.pid'] = $v['id'];
            $this->alias('a')->where($where1)
                    ->join($u->getTableName().' b', 'a.user_id=b.id', 'left');
            if($user_id > 0){
                $fields .= $is_like_fileds;
                $this->join($likeM->getTableName().' c','c.user_id='.$user_id.' and FIND_IN_SET(a.id,comment_ids)','left');
            }
            $list[$k]['children'] = $this->field($fields)->select();
        }
        return $list;
        
    }
    

}
